package com.zbkj.crmeb.finance.service.process;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.constants.Constants;
import com.constants.WeChatConstants;
import com.crmeb.core.exception.CrmebException;
import com.zbkj.crmeb.finance.model.UserRecharge;
import com.zbkj.crmeb.finance.service.UserRechargeService;
import com.zbkj.crmeb.front.request.UserRechargeRequest;
import com.zbkj.crmeb.front.response.OrderPayItemResponse;
import com.zbkj.crmeb.system.service.SystemConfigService;
import com.zbkj.crmeb.system.service.SystemGroupDataService;
import com.zbkj.crmeb.system.vo.SystemGroupDataRechargeConfigVo;
import com.zbkj.crmeb.user.service.UserService;


/**
 * @ClassName: OrangeRechargeProcess
 * @Description: 橙子充值订单
 */
@Service("orangeRechargeProcess")
public class OrangeRechargeProcess implements IRechargeOrderProcess {
    public static final Logger logger = LoggerFactory.getLogger(OrangeRechargeProcess.class);

    @Autowired
    private SystemConfigService systemConfigService;
    @Autowired
    private SystemGroupDataService systemGroupDataService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserRechargeService userRechargeService;

    @Value("${orange.appId}")
    private String appId;
    @Value("${orange.secret}")
    private String secret;
    @Value("${orange.url}")
    private String url;

    @Override
    public Map<String, Object> rechage(UserRechargeRequest request) {
        request.setPayments(Constants.PAY_TYPE_ORAGE_PAY);
        //验证金额是否为最低金额
        String rechargeMinAmountStr = systemConfigService.getValueByKeyException(Constants.CONFIG_KEY_RECHARGE_MIN_AMOUNT);
        BigDecimal rechargeMinAmount = new BigDecimal(rechargeMinAmountStr);
        int compareResult = rechargeMinAmount.compareTo(request.getPrice());
        if(compareResult > 0){
            throw new CrmebException("充值金额不能低于" + rechargeMinAmountStr);
        }
        if(request.getGroupDataId() > 0){
            SystemGroupDataRechargeConfigVo systemGroupData = systemGroupDataService.getNormalInfo(request.getGroupDataId(), SystemGroupDataRechargeConfigVo.class);
            if(null == systemGroupData){
                throw new CrmebException("您选择的充值方式已下架");
            }

            //售价和赠送
            request.setPrice(systemGroupData.getPrice());
            request.setGivePrice(BigDecimal.ZERO);
        }
        //生成系统订单
        request.setUserId(userService.getUserIdException());

        UserRecharge userRecharge = userRechargeService.create(request);

        OrderPayItemResponse itemResponse = getResponese(userRecharge);

        Map<String, Object> map = new HashMap<>();
        map.put("data", itemResponse);
        map.put("type", request.getFromType());
        return map;
    }

    private OrderPayItemResponse getResponese(UserRecharge userRecharge){
        OrderPayItemResponse itemResponse = new OrderPayItemResponse();
        //获取域名
        String domain = systemConfigService.getValueByKeyException(Constants.CONFIG_KEY_API_URL);
        itemResponse.setKey(userRecharge.getOrderId());
        itemResponse.setOrderId(userRecharge.getOrderId());
        itemResponse.setAppId(appId);
        itemResponse.setNotifyUrl(domain + WeChatConstants.PAY_NOTIFY_API_URI_RECHARGE);
        itemResponse.setAmount(userRecharge.getPrice());
        itemResponse.setOrderDesc("充值");
        itemResponse.setV("1.0");
        itemResponse.setT(System.currentTimeMillis());
        return itemResponse;
    }
}
